package org.example.rmsmsapi.service;
import org.example.rmsmsapi.Utils.DateUtils;
import org.example.rmsmsapi.model.suser;
import org.example.rmsmsapi.model.uRoles;
import org.example.rmsmsapi.sdao.smapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    private smapper apper;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        suser s1=new suser();
        s1=apper.GetUserByUserCode(userName);
        ArrayList<String> r=new ArrayList<>();
        r=apper.GetRolesByCode(s1.getUserCode());
//        uRoles u=new uRoles();
//        u.setRoles(r);
//        u.setUserCode(s1.getUserCode());
//        u.setCreateTime(s1.getCreateTime());
//        u.setStop(s1.getStop());
//        u.setHosId(s1.getHosId());
//        u.setLoginTime(s1.getLoginTime());
//        u.setUserName(s1.getUserName());
//        u.setUserId(s1.getUserId());
//        u.setUserType(s1.getUserType());
//        u.setToken(s1.getToken());
//        u.setAvatar(s1.getAvatar());
//        u.setIntroduction(s1.getIntroduction());
        //String date;
        //date=DateUtils. Getdnowdatexx();
        //apper.uPuser(s1.getUserId(), date);
       // s1.setLoginTime(date);
        // 查询用户
       // User user = userMapper.selectByUserName(userName);
        if (s1== null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        // 获取用户权限
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (r!= null) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + r));
        }

        // 返回Spring Security用户
        return new org.springframework.security.core.userdetails.User(
                s1.getUserName(),
                s1.getPassword(),
                true,
                true,
                true,
                true,
                authorities
        );
    }
}